/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the IncrementalParser package.
 *  
 *  The IncrementalParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.incparser.logging;

import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class HTMLWriter extends FileWriter {

    public HTMLWriter(String directory, String fileName) throws IOException {
        this(directory, fileName, fileName);
    }

    public HTMLWriter(String directory, String fileName, String title) throws IOException {
        super(directory + "/" + fileName);
        super.write("<html>\n<title>" + title + "</title>\n</head>\n<body>\n");
    }

    @Override
    public void close() throws IOException {
        super.write("\n</body>\n</html>\n");
        super.close();
    }

    public void openTag(String tag)  {
        openTag(tag, null);
    }

    @Override
    public void write(String string) {
        try {
            super.write(string);
        } catch (IOException ex) {
            Logger.getLogger(HTMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void openTag(String tag, String attributes)  {
        write("<" + tag
                + (attributes != null ? " " + attributes : "")
                + ">");
    }

    public void closeTag(String tag)  {
        write("</" + tag + ">");
    }

    public void emptyTag(String tag, String attributes)  {
        write("<" + tag + (attributes != null ? " " + attributes : "") + "/>");
    }

    public void writeTag(String tag, String text)  {
        writeTag(tag, null, text);
    }

    public void writeTag(String tag, String attributes, String text) {
        openTag(tag, attributes);
        write(text);
        closeTag(tag);
    }

    public void url(String url, String text) {
        writeTag("a", "href=" + url, text);
    }

    public void nl() {
        write("\n");
    }

    public void br() {
        openTag("br");
    }

    public void write(String x, int times) {
        for (int i = 0; i < times; ++i)
            write(x);
    }
}
